//
//  MineUserHead.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/30.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MineUserHead.h"
#import "MineItemModel.h"
@interface MineUserHead ()
@property(nonatomic, strong) UIImageView *userImageView;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) UILabel *dateLabel;
@property(nonatomic, strong) UIButton *editeButton;
@end

@implementation MineUserHead
- (void)makeUI {
    [super makeUI];
    self.backgroundColor = UIColor.clearColor;
    [self addSubview:self.userImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.dateLabel];
    [self addSubview:self.editeButton];
    
    [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.left.top.mas_equalTo(0);
    }];
    self.height = 60;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userImageView.mas_right).mas_offset(16);
        make.height.mas_equalTo(27);
        make.bottom.mas_equalTo(self.userImageView.mas_centerY).mas_offset(0);
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userImageView.mas_right).mas_offset(16);
        make.height.mas_equalTo(21);
        make.top.mas_equalTo(self.userImageView.mas_centerY).mas_offset(0);
    }];
    [self.editeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dateLabel.mas_right).mas_offset(4);
        make.size.mas_equalTo(CGSizeMake(16, 16));
        make.centerY.mas_equalTo(self.dateLabel);
    }];
}
- (void)handleEvent {
    [super handleEvent];
    [self.editeButton addTarget:self action:@selector(clickEditeButton) forControlEvents:UIControlEventTouchUpInside];
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickEditeButton)];
        self.userImageView.userInteractionEnabled = YES;
        [self.userImageView addGestureRecognizer:tap];
    }
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickEditeButton)];
        self.nameLabel.userInteractionEnabled = YES;
        [self.nameLabel addGestureRecognizer:tap];
    }
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickEditeButton)];
        self.dateLabel.userInteractionEnabled = YES;
        [self.dateLabel addGestureRecognizer:tap];
    }
}
- (void)bindViewModel {
    self.nameLabel.text = @"";
    self.dateLabel.text = @"设置婚期";
    [RACObserve([TTUserModel sharedObject], timeTag) subscribeNext:^(id x) {
        if ([TTUserModel sharedObject].marryTime.length > 0) {
            self.dateLabel.text = [NSString stringWithFormat:@"婚期：%@",[TTUserModel sharedObject].marryTime];
        } else {
            self.dateLabel.text = @"设置婚期";
        }
    }];
}
- (void)refresh {
    if ([TTUserModel sharedObject].token.length > 0) {
        self.nameLabel.text = [TTUserModel sharedObject].nickname;
        [self.userImageView sd_setImageWithURL:[TTUserModel sharedObject].userIcon.wppURL placeholderImage:[UIImage imageNamed:@"guide_usericon"]];
    } else {
        self.nameLabel.text = @"";
        self.dateLabel.text = @"设置婚期";
    }
}
#pragma mark - click
- (void)clickEditeButton {
    MineItemModel *itemModel = [[MineItemModel alloc]init];
    itemModel.action = @(ItemActionUserInfo);
    !self.clickAction ?: self.clickAction(itemModel);
}
#pragma mark - lazy
- (UIImageView *)userImageView {
    if (!_userImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.image = [UIImage imageNamed:ImageName.guide_usericon];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.layer.cornerRadius = 30;
        iv.layer.masksToBounds = YES;
        _userImageView = iv;
    }
    return _userImageView;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = kFontMedium(18);
        lab.textColor = rgba(47, 47, 52, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _nameLabel = lab;
    }
    return _nameLabel;
}
- (UILabel *)dateLabel {
    if (!_dateLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = kFontRegular(14);
        lab.textColor = rgba(137, 137, 145, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _dateLabel = lab;
    }
    return _dateLabel;
}
- (UIButton *)editeButton {
    if (!_editeButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:ImageName.mine_edit] forState:UIControlStateNormal];
        _editeButton = button;
    }
    return _editeButton;
}
@end
